@{extends '../@layout.phtml'}

@{block #custom_head}
<link rel="stylesheet" type="text/css" href="{$basePath}/css/jquery-ui/normal/default.css" />
<link rel="stylesheet" media="screen,projection,tv" href="{$basePath}/systemApps/celebrioweather/default.css" type="text/css" />
<script type="text/javascript" src="{$basePath}/js/libs/jquery-ui.js"></script>
@{/block}

@{block #content}
<script type="text/javascript">
    var basePath = {$basePath};
    butonize("input:submit");
    $(document).ready(function(){
        runCelebrioKeyboard({$full_lang});
        if (typeof document.body.onselectstart!="undefined")
            document.body.onselectstart=function(){ return false }
        else if (typeof document.body.style.MozUserSelect!="undefined")
            document.body.style.MozUserSelect="none";
        $(document.body).bind("contextmenu", function(e) {
            e.preventDefault();
            return false;
        });
    });
    var woeid = null;
    
    function updateWeatherData(latitude, longitude, place){
        if(!place) {
            var place = "";
        }
        $.getJSON( {link weather}, { latitude: latitude, longitude: longitude, place: place }, function (payload) {
            $("#celebrioweatherapp").html(payload.snippets["snippet--celebrioweatherappSnippet"]);
            butonize("input:submit");
        });
    }
    //youtech uvnitř modulů vhodné implicitní chování typu:
    function getWeatherForCity(cityName) {
        //var address ="http://where.yahooapis.com/v1/places.q("+cityName+")?appid=U7bhIjTV34GhrFPaCCvJGh59NQjwDSO2VFnZ.nUl9YazBNaL48EGTH0RWa3lzc45BgzKz8Q&format=json&callback=?";
        // if(data.places.count !== 0) {
        updateWeatherData(null, null, cityName);
        setTimeout(function(){
            updateWeatherData(null, null, cityName);
        }, 1800000); // every half an hour
                     
        //                    } else {
        //                        if(cityName.length > 20) {
        //                            cityName = cityName.substring(0,20);
        //                            cityName = '"'+cityName+"..."+'"';
        //                        }
        //                        $(".celebrioweatherapp_cityNotFound").html("City "+cityName+" was not found");
        //                    }
    }
    
    function getWeatherForCurrentLocation() {
        if(navigator.geolocation) {
            browserSupportFlag = true;
            navigator.geolocation.getCurrentPosition(function(position) {
                $( "#progressbar" ).progressbar({ value: 1 });
                var latlng = position.coords.latitude + "," + position.coords.longitude;
                var latitude = position.coords.latitude;
                var longitude = position.coords.longitude;
                var apiurl = 'http://maps.google.com/maps/geo?output=json&sensor=false&q=' + latlng;
                $("#progressbar").progressbar("option","value",5);
                $("#progressbar").progressbar("option","value",10);
                updateWeatherData(latitude, longitude);
                setTimeout(function(){
                    updateWeatherData(latitude, longitude);
                }, 1800000); // every half an hour
                //                $.getJSON(yqlurl, function(data) {
                //                    var subdata = data.query.results.json.Placemark.AddressDetails.Country.AdministrativeArea;
                //                    $("#progressbar").progressbar("option","value",15);
                //                    if (subdata.SubAdministrativeArea) {
                //                        cityName = subdata.SubAdministrativeArea.SubAdministrativeAreaName;
                //                    } else {
                //                        cityName = subdata.AdministrativeAreaName;
                //                    }
                //                    var address ="http://where.yahooapis.com/v1/places.q("+cityName+")?appid=U7bhIjTV34GhrFPaCCvJGh59NQjwDSO2VFnZ.nUl9YazBNaL48EGTH0RWa3lzc45BgzKz8Q&format=json&callback=?";
                //                    $("#progressbar").progressbar("option","value",30);
                //                    $.getJSON(address, function(data){
                //                        console.log(data);
                //                        woeid = typeof data.places.place == "undefined" ? 818717 : data.places.place[0].woeid;
                //                        updateWeatherData();
                //                        $("#progressbar").progressbar("option","value",99);
                //        //$("#progressBarParent").remove();
                //                        setTimeout(function(){
                //                            updateWeatherData(woeid);
                //                        }, 1800000); // every half an hour
                //                    });
                //                    $("#progressbar").progressbar("option","value",60);
                //                });
            }, function errorHandler(err) {
                if(err.code == 1) {
                    alert("Error: Access is denied!");
                }else if( err.code == 2) {
                    alert("Error: Position is unavailable!");
                }
            });
        } else {
            var full_lang = {$full_lang};
            var default_city = "NY";
            switch(full_lang){
                case "en_US":
                case "en_EN":
                    default_city = "NY";
                    break;
                case "cs_CZ":
                    default_city = "Prague";
                    break;
                case "sk_SK":
                    default_city = "Bratislava";
                    break;
                default:
                    default_city = "NY";
                    break;
            }
            var address ="http://where.yahooapis.com/v1/places.q("+default_city+")?appid=U7bhIjTV34GhrFPaCCvJGh59NQjwDSO2VFnZ.nUl9YazBNaL48EGTH0RWa3lzc45BgzKz8Q&format=json&callback=?";
            $.getJSON(address, function(data){
                woeid = data.places.place[0].woeid;
                updateWeatherData();
                setTimeout(function(){
                    updateWeatherData(woeid);
                }, 1800000); // every half an hour
            });
        }
    }
    
    $(window).load(getWeatherForCurrentLocation());
</script>
<div class="celebrioweatherapp" id="celebrioweatherapp">
    {snippet celebrioweatherappSnippet}
    <h1>{!_"Weather is loading"}</h1>
    <div id="progressBarParent"><div id="progressbar"></div></div>
    {/snippet}
     <script type="text/javascript">
        $("#celebrioweatherapp_cityFinder").live("submit", function(event){
            event.preventDefault();
            var cityName = $("#celebrioweatherapp_cityField").val();
            if(cityName !== "") {
                getWeatherForCity(cityName);
                
            }
        });
        
        $("#celebrioweatherapp_homeWeather").live("submit", function(event){
            event.preventDefault();
            getWeatherForCurrentLocation();
            
        });
    </script>
</div>
{/block}